package com.dragon.client.controller.finance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.finance.ILoanService;
import com.dragon.repository.domain.dto.LoanDTO;
import com.dragon.repository.domain.vo.LoanVO;
import com.dragon.repository.entity.Loan;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 借款表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-03
 */
@RestController
@RequestMapping("loan")
@Api(tags = "借款管理")
public class LoanController {

    @Autowired
    ILoanService service;

    @GetMapping("list")
    @ApiOperation("获取借款列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数", required = false, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "审核", required = false, dataType = "int"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, type = "string"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, type = "string")
    })
    public ResponseData<List<LoanDTO>> reimburseList(@RequestParam Integer page, @RequestParam Integer limit,
                                                     @RequestParam(required = false) String search,
                                                     @RequestParam(required = false) Integer status,
                                                     @RequestParam(required = false) String startTime,
                                                     @RequestParam(required = false) String endTime){
        Page<Loan> rePage = service.loanList(page, limit, search,status, startTime, endTime);
        return ResponseData.success(LoanDTO.convertList(rePage.getRecords()), rePage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("新增借款")
    @AddPage
    public ResponseData addLoan(@RequestBody @Validated(Insert.class) LoanVO vo) {
        return ResponseData.success(service.addLoan(LoanVO.convert(vo)));
    }

    @PostMapping("update")
    @ApiOperation("修改借款")
    public ResponseData updateLoan(@RequestBody @Validated(Update.class)LoanVO vo) {
        service.updateLoan(LoanVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除借款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "报销id", required = true, type = "int"),
    })
    public ResponseData deleteLoan(@RequestParam Integer id){
        service.deleteLoan(id);
        return ResponseData.success();
    }

    @PostMapping("listById")
    @ApiOperation("根据id查看详情")
    public ResponseData<LoanDTO> getLoanById( @RequestParam
                                     @ApiParam(name = "id", value = "报销id", required = true, type = "int") Integer id){
        Loan loan = service.getLoanById(id);
        return ResponseData.success(LoanDTO.convert(loan));
    }

    @GetMapping("listStatistics")
    @ApiOperation(value = "列表统计")
    public ResponseData selectListStatistics(){
        return ResponseData.success(service.selectListStatistics());
    }
}
